package dm71.espil.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import dm71.espil.R;
import dm71.espil.REST.CreateUserParameters;
import dm71.espil.REST.DataService;
import dm71.espil.REST.Tasks.CreateUserTask;

public class OpretProfilActivity extends Activity 
{
	EditText editBrugernavn;
	EditText editPassword;
	EditText editName;
	EditText editEfternavn;
	EditText editKlasse;
	TextView textViewFejl;
	OpretProfilActivity context = this;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.opretprofil);
        
        Button butGem = (Button) findViewById(R.id.butGem);
    	editBrugernavn = (EditText) findViewById(R.id.editTextOpretPUsername);
    	editPassword = (EditText) findViewById(R.id.editTextOpretPPassword);
    	editName = (EditText) findViewById(R.id.editOpretPName);
    	editEfternavn = (EditText) findViewById(R.id.editOpretPLastname);
    	editKlasse = (EditText) findViewById(R.id.editOpretPClass);
        textViewFejl = (TextView) findViewById(R.id.textViewOpretPFejl);
    	
        butGem.setOnClickListener(new OnClickListener()
        {

        	public void onClick(View v)
        	{
        		DataService ds = new DataService();
        		
        		String username = editBrugernavn.getText().toString();
        		String password = editPassword.getText().toString();
        		
        		CreateUserParameters parameters = new CreateUserParameters(username, password, editName.getText().toString(), editEfternavn.getText().toString(), editKlasse.getText().toString());
        		
        		CreateUserTask createUser = new CreateUserTask(context);
        		createUser.execute(parameters);
        	}
        });
    }
	
	public void CreateUserCompleted(Boolean result)
	{
		if(result)
		{
			Intent intent = new Intent(context, LoginActivity.class);
			finish();
			startActivity(intent);
		}
		else
		{
			Resources re = getResources();
			String createUserFejl = re.getString(R.string.createUserFejl);
			textViewFejl.setText(createUserFejl);
		}
	}
}
